package org.black.phoneUtility.sms.reply.app.service;

import org.black.phoneUtility.sms.reply.service.SmsReplyService;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

public class SmsReplyScheduleUpdateSmsReplyService extends IntentService {
	private static final String TAG = SmsReplyScheduleUpdateSmsReplyService.class
			.getName();
	public static final String ARGUMENTS_SMS_REPLY_ID = "arguments_sms_reply_id";

	public static final String INTENT_ACTION_SCHEDULE_UPDATE_NOTIFICATION = "org.black.phoneUtility.intent.action.SCHEDULE_UPDATE_NOTIFICATION";

	private static final String LOG_TAG = SmsReplyScheduleUpdateSmsReplyService.class
			.getName();
	private SmsReplyService smsReplyService;
	private WakeLock wakeLock;

	public SmsReplyScheduleUpdateSmsReplyService() {
		super(SmsReplyScheduleUpdateSmsReplyService.class.getName());
	}

	@Override
	public void onCreate() {
		super.onCreate();
		this.smsReplyService = new SmsReplyService(this);
		PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
		this.wakeLock = powerManager.newWakeLock(
				PowerManager.PARTIAL_WAKE_LOCK, TAG);
		this.wakeLock.acquire();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.smsReplyService = null;
		if (this.wakeLock != null && this.wakeLock.isHeld() == true) {
			this.wakeLock.release();
			this.wakeLock = null;
		}
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		long smsReplyId = intent.getLongExtra(ARGUMENTS_SMS_REPLY_ID, 0);
		if (smsReplyId != 0) {
			this.smsReplyService.turnOffSmsReply(smsReplyId);
		} else {
			Log.e(LOG_TAG, "Sms Reply Id is 0!");
		}

	}
}
